package com.ourteam.product.model.api;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import com.ourteam.product.model.domain.ProductModelCodeTemplateBean;
import com.ourteam.product.model.domain.ProductModelObjectBean;
import com.ourteam.product.model.domain.ProductModelPackageBean;

public class ProductModelCodeGenerator {

	private static final VelocityEngine VELOCITY_ENGINE = new VelocityEngine();

	public static String[] generateProductModelObjectCode(ProductModelObjectBean modelObjectBean,
			ProductModelCodeTemplateBean codeTemplateBean, Map<String, Object> vmContenxt) throws Exception {
		return generateProductModelObjectCode(VELOCITY_ENGINE, modelObjectBean, codeTemplateBean, vmContenxt);
	}

	public static String[] generateProductModelObjectCode(VelocityEngine velocityEngine,
			ProductModelObjectBean modelObjectBean, ProductModelCodeTemplateBean codeTemplateBean,
			Map<String, Object> vmContenxt) throws Exception {

		Map<String, Object> contenxt = new HashMap<String, Object>();

		if (vmContenxt != null && vmContenxt.isEmpty() == false) {
			contenxt.putAll(vmContenxt);
		}

		contenxt.put("generateDate", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));

		contenxt.put("codeTemplate", codeTemplateBean);

		contenxt.put("stringUtils", StringUtils.class);

		contenxt.put("modelObject", new ProductModelObjectHelper(modelObjectBean));

		contenxt.put("filePath", codeTemplateBean.getFileOutputPath());

		StringWriter fileNameWriter = new StringWriter();

		velocityEngine.evaluate(new VelocityContext(contenxt), fileNameWriter, "code Template",
				new StringReader(codeTemplateBean.getFileNameTemplate()));

		contenxt.put("fileName", fileNameWriter.toString());

		StringWriter codeWriter = new StringWriter();

		//System.out.println(codeTemplateBean.getFileContentTemplate());

		velocityEngine.evaluate(new VelocityContext(contenxt), codeWriter, "code Template",
				codeTemplateBean.getFileContentTemplate());

		return new String[] { fileNameWriter.toString(), codeWriter.toString() };
	}

	public static String[] generateProductModelPackageCode(ProductModelPackageBean modelPackageBean,
			ProductModelObjectBean[] modelObjectBeans, ProductModelCodeTemplateBean codeTemplateBean,
			Map<String, Object> vmContenxt) throws Exception {

		return generateProductModelPackageCode(new VelocityEngine(), modelPackageBean, modelObjectBeans,
				codeTemplateBean, vmContenxt);
	}

	public static String[] generateProductModelPackageCode(VelocityEngine velocityEngine,
			ProductModelPackageBean modelPackageBean, ProductModelObjectBean[] modelObjectBeans,
			ProductModelCodeTemplateBean codeTemplateBean, Map<String, Object> vmContenxt) throws Exception {

		Map<String, Object> contenxt = new HashMap<String, Object>();

		if (vmContenxt != null && vmContenxt.isEmpty() == false) {
			contenxt.putAll(vmContenxt);
		}

		contenxt.put("generateDate", DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss"));

		contenxt.put("codeTemplate", codeTemplateBean);

		contenxt.put("stringUtils", StringUtils.class);

		ProductModelObjectHelper[] modelObjectHelpers = new ProductModelObjectHelper[modelObjectBeans.length];

		for (int i = 0; i < modelObjectHelpers.length; i++) {
			modelObjectHelpers[i] = new ProductModelObjectHelper(modelObjectBeans[i]);
		}

		contenxt.put("modelPackage", new ProductModelPackageHelper(modelPackageBean));

		contenxt.put("modelObjects", modelObjectHelpers);

		contenxt.put("filePath", codeTemplateBean.getFileOutputPath());

		StringWriter fileNameWriter = new StringWriter();

		velocityEngine.evaluate(new VelocityContext(contenxt), fileNameWriter, "code Template",
				new StringReader(codeTemplateBean.getFileNameTemplate()));

		contenxt.put("fileName", fileNameWriter.toString());

		StringWriter codeWriter = new StringWriter();

		velocityEngine.evaluate(new VelocityContext(contenxt), codeWriter, "code Template",
				codeTemplateBean.getFileContentTemplate());

		return new String[] { fileNameWriter.toString(), codeWriter.toString() };
	}
}
